package Checkers;

public class DNA
{
    private final int standard = 1000;
    private final int[] poly0 = {standard,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 ,0,0,0,0};
    private int[] poly;
    public int age,wins,losses,draws; //public for easier access
    private final int randFactor = 50;
    private final int numTerms = poly0.length;
    private boolean dead;
    public boolean survivor; //flag to make the cutoff function work properly
    
//    public final int[] discard={2,4,7,13,14,16,19}; //set these weights to 0
    public final int[] discard={23};  
  
    public DNA()
    {
        survivor=true;
        dead=false;
        age=0;wins=0;losses=0;draws=0;
        zero();
    }
    
    public DNA(DNA d)
    {
        survivor=d.survivor;
        dead=d.isDead();
        age=d.age;
        wins=d.wins;
        losses=d.losses;
        draws=d.draws;
        poly=d.getPoly();
    }

    public int size()
    {
        return numTerms;
    }
    
    public void die()
    {
        dead=true;
        survivor=false;
    }
    
    public void revive()
    {
        dead=false;
        age=0;
    }
    
    public boolean isDead()
    {
        return dead;
    }
    
    public void zero()
    {
        poly=poly0.clone();
        age=0;
    }
    
    public int[] getPoly()
    {
        return poly.clone();
    }
    
    public int getWeight(int w)
    {
        return poly[w];
    }
    
    public void resetScore()
    {
        wins=0;losses=0;draws=0;
        survivor=true;
    }
    
    public void mutate()
    {
        for(int j=0;j<poly.length;j++)
        {
            if(Math.random()<0.5)
                poly[j]+=(int)((Math.random()*randFactor));
            else
                poly[j]-=(int)((Math.random()*randFactor));
        }
        
        poly[0]=standard;
        discard();
    }

    public void mutate(int[] variance)
    {
        for(int j=0;j<poly.length;j++)
        {
            int randFac=variance[j];
            
            if(Math.random()<0.5)
                poly[j]+=(int)((Math.random()*randFac));
            else
                poly[j]-=(int)((Math.random()*randFac));
        }
        
        poly[0]=standard;
        discard();
    }
    
    private void discard()
    {
        for(int i=0;i<discard.length;i++)
        {
            poly[discard[i]]=0;
        }
    }
    
    public void setPoly(int[] poly1)
    {
        poly=poly1.clone();
    }

    public int getScore()//calculate fitness (higher=better)
    {
        return 3*wins+draws-3*losses;
    }
    
    public String stats(String i)
    {
        String s="Player "+i+" | Age = "+age+" | Score = "+getScore()+" | DNA: ";
        for(int j=0;j<poly.length;j++)
        {
            s+=poly[j]+", ";
        }
        return s; 
    }
}
